# Makefile for dfileana
# Copyright (C) 2010 drangon <drangon.zhou@gmail.com>
# 2010-09
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


HOST_TYPE ?= linux
QT_DIR ?= /home/drangon/opt


APP_TARGET=dfileana.exe

SRCS=$(wildcard *.cpp)
OBJS=$(foreach src, $(SRCS), $(basename $(src)).o)

MOC_HEADERS = DFileAnaWin.h FileHexWidget.h FileInfoWidget.h
MOC_SRCS=$(foreach h, $(MOC_HEADERS), moc/moc_$(basename $(h)).cpp)
MOC_OBJS=$(foreach h, $(MOC_HEADERS), moc/moc_$(basename $(h)).o)

UI_SRCS = 
UI_HEADERS = $(foreach h, $(UI_SRCS), moc/$(basename $(h)).h)

RCC_QRCS = 
RCC_SRCS = $(foreach r, $(RCC_QRCS), moc/rcc_$(basename $(r)).cpp)
RCC_OBJS = $(foreach r, $(RCC_QRCS), moc/rcc_$(basename $(r)).o)


CXX=g++
CFLAGS = -g -Wall -pipe -I$(QT_DIR)/include -I$(QT_DIR)/include/QtCore
CFLAGS += -I$(QT_DIR)/include/QtGui -I$(QT_DIR)/include/QtWidgets
LDFLAGS = -L$(QT_DIR)/lib -lQt5Widgets -lQt5Gui -lQt5Core

mingw32_LDFLAGS = -mwindows -lqtmain -lAdvapi32 -lcomctl32 -lcomdlg32 -lole32 -lgdi32 -luuid
mingw64_LDFLAGS = $(mingw32_LDFLAGS)
linux_LDFLAGS = -lXext -lX11 -lm

LDFLAGS += $($(HOST_TYPE)_LDFLAGS)


.PHONY : all clean copy_dll dist
	
all : $(APP_TARGET) 

$(APP_TARGET) : $(OBJS) $(MOC_OBJS) $(RCC_OBJS)
	$(CXX) $(CFLAGS) -o $@ $(OBJS) $(MOC_OBJS) $(RCC_OBJS) $(LDFLAGS)


clean :
	rm -f $(APP_TARGET) $(OBJS) $(MOC_OBJS) $(RCC_OBJS)

mingw32_LIBGCC_NAME = libgcc_s_sjlj-1.dll
mingw64_LIBGCC_NAME = libgcc_s_seh-1.dll

copy_dll :
	cp $(MINGW_DIR)/lib/$($(HOST_TYPE)_LIBGCC_NAME) .
	cp $(MINGW_DIR)/bin/libstdc++-6.dll .
	cp $(QT_DIR)/lib/Qt5Core.dll .
	cp $(QT_DIR)/lib/Qt5Gui.dll .
	cp $(QT_DIR)/lib/Qt5Widgets.dll .
	cp $(QT_DIR)/plugins/imageformats/qico.dll ./imageformats/
	cp $(QT_DIR)/plugins/imageformats/qgif.dll ./imageformats/
	cp $(QT_DIR)/plugins/imageformats/qjpeg.dll ./imageformats/
	cp $(QT_DIR)/plugins/platforms/qwindows.dll ./platforms/

dist : $(APP_TARGET) copy_dll
	strip $(APP_TARGET)
	strip $(wildcard *.dll)
	strip $(wildcard ./imageformats/*.dll)
	strip $(wildcard ./platforms/*.dll)

$(MOC_SRCS) : moc/moc_%.cpp : %.h
	moc -o $@ $<

$(UI_HEADERS) : moc/%.h : %.ui
	uic -o $@ $<

$(RCC_SRCS) : moc/rcc_%.cpp : %.qrc
	rcc -compress 5 -o $@ $<

$(OBJS) : $(UI_HEADERS)


.SUFFIXES : .cpp

.cpp.o:
	$(CXX) $(CFLAGS) -c -o $@ $<


DEPS=$(foreach src, $(SRCS), $(basename $(src)).zzdep)
-include $(DEPS)

